﻿angular.module('starter')
.controller("homeCtrl", function ($scope, $rootScope, appBase, httpService,$timeout,UserInfo,$ionicHistory) {

    initialize();

    $scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
        if (fromState.url == "selectArea") {
            initialize();
            console.log("返回页面");
        }
    });
    
	$scope.$on('$ionicView.enter', function() {
		$ionicHistory.removeBackView();//移除当前视图的前一个视图，包括缓存元素和范围

		$ionicHistory.clearHistory();
		$ionicHistory.currentView()

	});
	
	$scope.$on('$ionicView.loaded', function() {
		BackOutApp(function(){
			$ionicHistory.goBack();
			});
	});

    $scope.do_infinite = function () {
 
        if ($scope.hasMore) {
            $scope.hasMore = false;
           // $scope.$broadcast("scroll.infiniteScrollComplete");
            loadHospital();
            $scope.$broadcast("scroll.infiniteScrollComplete");
            //return;
        }
    }

    $scope.do_refresher = function () {

        $scope.hospitalArray = [];
        $scope.$broadcast("scroll.refreshComplete");//下拉停止
    }
 
    //初始化
    function initialize()
    {

        //当前第几页
        $scope.currentPage = 1;
        //是否还有更多
        $scope.hasMore = true;
        //医院的信息集合
        $scope.hospitalArray = [];
        $scope.UserInfo = UserInfo;

        if (appBase.cityName == null) {
            $scope.cityResult = "广州市";
        } else {
            $scope.cityResult = appBase.cityName;
        }
        loadHospital(true);
    }

    //加载数据
    function loadHospital(isInIt) {
        isInIt = arguments[0] || false;
        httpService.parData.clear();
        var parData = httpService.parData;
        parData.page = 3;

        if (isInIt) {
            $scope.currentPage = 1;
            $("#hospitalTile").html("——&nbsp;&nbsp;向下滑更多 &nbsp;&nbsp;——");
            $scope.hasMore = true;
            $scope.hospitalArray = [];
        }
        parData.cityCode = appBase.cityCode;
        parData.currentPage = $scope.currentPage;
        parData.sortType = 0;
        parData.isHot = 1;

        httpService.post(httpService.appWebApi.appGetHospital,
          parData
          ).then(
          function (success) {
              var data = success.data;
              if (data.returnCode != "0") {
                  console.log(data.returnMsg)
                  return;
              }
              $scope.currentPage = $scope.currentPage + 1;
              angular.forEach(data.returnData, function (modal) {
                  $scope.hospitalArray.push(
                      {
                          "name": modal.hsp_Name,
                          "grade": modal.hospitalLevel,
                          "hopId": modal.hsp_Code,
                          "vis": "5855",
                          "imgsrc": modal.pic,
                          "addr": modal.addr,
                          "items": getHospitalItems(modal)

                      });
              });
              if (data.returnData==null||data.returnData.length < 2) {
                  $("#hospitalTile").html("——&nbsp;&nbsp;没有更多 &nbsp;&nbsp;——");
                  $scope.hasMore = false;
              } else {
                  $scope.hasMore = true;
              }
          }
          , function (error) {
              console.log(error);
              $scope.hasMore = true;
          }).finally(function () {
              $rootScope.$digest();
          });
    }
})